<?php


namespace app\admin\controller;

use app\admin\model\Article as ArticleModel;
use think\response\Json;

/**
 * Class Article
 * @package app\admin\controller
 * @author Mr.Liu Wuchen
 * @create 2021-06-23 15:22
 */
class Article extends ApiCommon
{

    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @author Mr.Liu Wuchen
     * @create 2021-05-29 9:39
     */
    public function initialize()
    {
        $action = [
            'permission' => ['info'],
            'allow'      => ['category']
        ];
        event('CheckAuth', $action);
        $request = $this->request;
        $a       = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }


    /**
     * 无权限的文章详情
     * @param ArticleModel $articleModel
     * @return Json
     * @author Mr.Li
     * @create 2021-09-02 21:03
     */
    public function info(ArticleModel $articleModel): Json
    {
        $data = request()->param();
        $data['article_id'] = $data['article_id'] ?? 0;
        $data = $articleModel->getDataInfo($data);
        if($data){
            $data['content'] = '<html><body>'.$data['content'].'</body></html>';
        }
        if (!$data) {
            return resultArray(['error' => $articleModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }


    /**
     * 列表
     * @param  ArticleModel $articleModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-06-23 13:43
     */
    public function list(ArticleModel $articleModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $articleModel->getDataList($this->param);
        if (!$data) {
            return resultArray(['error' => $articleModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 分类添加
     * @param  ArticleModel $articleModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-06-23 13:44
     */
    public function create(ArticleModel $articleModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $articleModel->addData($this->param);
        if (!$data) {
            return resultArray(['error' => $articleModel->getError()]);
        }
        return resultArray(['error' => $articleModel->getError(), 'data'=>[]]);
    }

    /**
     * 编辑
     * @param  ArticleModel $articleModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-06-23 13:44
     */
    public function update(ArticleModel $articleModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $articleModel->saveData($this->param);
        if (!$data) {
            return resultArray(['error' => $articleModel->getError()]);
        }
        return resultArray(['error' => $articleModel->getError(), 'data'=>[]]);
    }

    /**
     * 详情
     * @param  ArticleModel $articleModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-06-23 14:09
     */
    public function read(ArticleModel $articleModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $articleModel->getDataInfo($this->param);
        if (!$data) {
            return resultArray(['error' => $articleModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 删除
     * @param  ArticleModel $articleModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-06-23 15:07
     */
    public function delete(ArticleModel $articleModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $articleModel->deleteData($this->param);
        if (!$data) {
            return resultArray(['error' => $articleModel->getError()]);
        }
        return resultArray(['error' => $articleModel->getError(), 'data'=>[]]);
    }

    /**
     * 全部分类
     * @param  ArticleModel $articleModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-06-23 16:59
     */
    public function category(ArticleModel $articleModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $articleModel->getCategoryDataList($this->param);
        if (!$data) {
            return resultArray(['error' => $articleModel->getError()]);
        }
        return resultArray(['error' => $articleModel->getError(), 'data'=>$data['data']]);
    }

    /**
     * 操作记录
     * @param  ArticleModel $articleModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-06-24 8:40
     */
    public function record(ArticleModel $articleModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $articleModel->getRecordList($this->param);
        if (!$data) {
            return resultArray(['error' => $articleModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

}
